import Dialog from 'tdesign-miniprogram/dialog/index';
import { mainApiDomain } from '../config';
const appData = getApp();
Component({
  data: {
    selectedTab: 'home',
    isHideTabBar: false,
    "list": [{
      "name": "organization",
      "pagePath": "/pages/organization/organization",
      "selectedIconPath": "/assets/images/tab-icons/tab1-select.png",
      "iconPath": "/assets/images/tab-icons/tab1-unselect.png",
      "text": "组织"
    },
    {
      "name": "class",
      "pagePath": "/pages/class/class",
      "selectedIconPath": "/assets/images/tab-icons/tab2-select.png",
      "iconPath": "/assets/images/tab-icons/tab2-unselect.png",
      "text": "班级"
    },
    {
      "name": "home",
      "pagePath": "/pages/home/home",
      "selectedIconPath": "",
      "iconPath": "",
      "text": "首页"
    },
    {
      "name": "activity",
      "pagePath": "/pages/activity/activity",
      "selectedIconPath": "/assets/images/tab-icons/tab4-select.png",
      "iconPath": "/assets/images/tab-icons/tab4-unselect.png",
      "text": "活动"
    },
    {
      "name": "mine",
      "pagePath": "/pages/mine/mine",
      "selectedIconPath": "/assets/images/tab-icons/tab5-select.png",
      "iconPath": "/assets/images/tab-icons/tab5-unselect.png",
      "text": "我的"
    }
    ],
    showLoginConfirm: false,
    showConfirm: false
  },
  methods: {
    tabbarClick: function (e) {
      let { item } = e.currentTarget.dataset;

      // wx.switchTab({
      //   url: item.pagePath
      // });
      wx.setStorageSync('apiAccessToken', '111')
      const { fellowId } = wx.getStorageSync('userInfo')
      console.log(fellowId);
      if (!wx.getStorageSync('apiAccessToken')) {
        // this.wxLogin()
        this.setData({ showLoginConfirm: true })
      } else if (['class', 'organization'].includes(item.name) && !fellowId) {
        this.setData({ showConfirm: true })
      } else {
        wx.switchTab({
          url: item.pagePath
        });
      }
    },
    init() {
      const page = getCurrentPages().pop();
      this.setData({
        selectedTab: this.data.list.find(item => item.pagePath === `/${page.route}`).name
      });
    },
    hide(e) {
      this.setData({ isHideTabBar: e })
    },
    onTapConfirm() {
      wx.navigateTo({ url: '/pages/authentication-auto/authentication-auto', })
    },
    closeDialog() {
      this.setData({ showConfirm: false, showLoginConfirm: false })
    },
    showLoginConfirm() {
      const _this = this
      wx.login({
        success: (detail) => {
          wx.request({
            method: 'GET', timeout: 5000, url: `${mainApiDomain}/wechat/LoginToMiniWx`,
            data: Object.assign({ code: detail.code }),
            success(res) {
              const passCode = [200]
              if (passCode.includes(res.data.code)) {
                wx.setStorageSync('apiAccessToken', res.data.data.wxToken)
                wx.setStorageSync('userInfo', res.data.data)
              } else {
                wx.showToast({ icon: "none", title: res.data.message })
              }
              // return true; // 关闭弹窗
              _this.closeDialog()
            },
            fail(res) {
              reject(res)
              wx.showToast({
                title: '服务器异常',
                icon: 'none'
              })
            },
            complete(res) {
            }
          })
        },
      })
    },
    showWxLoginDialog(code) {
      const _this = this
      const dialogConfig = {
        context: this,
        content: '请登录',
        cancelBtn: '取消',
        confirmBtn: {
          openType: 'chooseAvatar',
          content: '去登录',
          bindchooseavatar({ detail }) {
            console.log(detail);
            if (detail.errMsg.includes('fail')) {
              console.log('获取失败');
              wx.showToast({
                icon: 'none',
                title: '获取失败，请重试',
              })
              return false; // 不关闭弹窗
            } else {
              appData.userInfo = detail.userInfo
              wx.setStorageSync('apiAccessToken', 'data')
              return _this.showLoginConfirm({ code, encryptedData: detail.encryptedData, iv: detail.iv, })
            }
          },

        },
      };

      Dialog.confirm(dialogConfig)
        .then(() => {
          console.log('点击确定');
        })
        .catch(() => {
          console.log('点击取消');
        });
    },
  },
});